package com.liangzai.controller;

import com.liangzai.factory.FactoryHandler;
import com.liangzai.handler.GatewayHandler;
import com.liangzai.service.impl.GatewayHandlerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author leiliang
 * @version 1.0
 * @Description 责任连模式入口
 * @date 2021/3/11 16:20
 */
@RestController
public class HandlerController {

    @Autowired
    GatewayHandlerService gatewayHandlerService;


    @RequestMapping("/handler")
    public String handler() {
        GatewayHandler gatewayHandler = FactoryHandler.getGatewayHandler();
        gatewayHandler.service();
        return "success";
    }

    /**
     * 具体实现:
     * 数据库查询方式:
     * 1.定义GatewayHandler抽象角色
     * 2.定义不同的Handler类
     * 3.查询出第一个handler-id为null的数据，获取到下级的handler-id
     * 4.下级的handler-id存放到GatewayHandler中
     * 5.最后执行具体的抽象service
     * @return
     */
    @RequestMapping("/gethandler")
    public String gethandler() {
        GatewayHandler dbGatewayHandler = gatewayHandlerService.getDbGatewayHandler();
         dbGatewayHandler.service();
        return "success";
    }
}
